import $ from 'jquery'
import Vue from 'vue'
import App from './App'
import router from './router'
import 'element-ui/lib/theme-chalk/index.css'
import ElementUI from 'element-ui'
import axios from 'axios'


axios.defaults.withCredentials = true;
let http = axios.create();
//设置请求拦截器
http.interceptors.request.use(
  config => {
    //从sessionStorage中获取token，添加到请求头
    let token = window.sessionStorage.getItem("token");
    let refresh = window.sessionStorage.getItem("refresh");
    //设置到请求头
    config.headers.authentication = token;
    config.headers.refresh = refresh;
    return config;
  }
);
//响应拦截器
http.interceptors.response.use(
  response => {
    //得到token、refreshToken并存放到本地
    let token = response.headers.authentication;
    let refresh = response.headers.refresh;
    //判断token是否为空
    if (token != null) {
      //localStorage 永久存在
      window.sessionStorage.setItem("token", token);
      //浏览器关闭数据丢失
    }
    if (refresh != null) {
      window.sessionStorage.setItem("refresh", refresh);
    }
    return response;
  }
);
Vue.config.productionTip = false
Vue.use(ElementUI);
Vue.prototype.$axios = axios;
Vue.config.productionTip = false;

Vue.prototype.$axios = http   //设置成元属性

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  render: h => h(App)
})
